package ppackage;

import java.util.Random;

public class Phone {

    public static void main(String[] args) {
        Receiver receiver = new Receiver("接收者");

        Caller caller1 = new Caller("张三", "你好吗?", receiver);
        Caller caller2 = new Caller("李四", "祝你万事如意", receiver);
        Caller caller3 = new Caller("王二", "新年快乐", receiver);

        Thread thread1 = new Thread(caller1);
        Thread thread2 = new Thread(caller2);
        Thread thread3 = new Thread(caller3);

        thread1.start();
        thread2.start();
        thread3.start();
    }
}

class Receiver {
    private String name;

    public Receiver(String name) {
        this.name = name;
    }

    public void answerCall(String callerName, String message, int duration) {
        System.out.println("我在按" + callerName + "打来的电话，他对我说:" + message + "，我在和他交谈。");
        try {
            Thread.sleep(duration * 1000); // 模拟通话过程
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("我挂掉" + callerName + "的电话。");
    }
}

class Caller implements Runnable {
    private String name;
    private String message;
    private Receiver receiver;

    public Caller(String name, String message, Receiver receiver) {
        this.name = name;
        this.message = message;
        this.receiver = receiver;
    }

    public void run() {
        Random random = new Random();
        int duration = random.nextInt(21) + 10; // 生成10到30秒之间的随机数
        receiver.answerCall(name, message, duration);
    }
}
